package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;

@Configuration  //点进源码可以发现该注解等价于Component
//开启swagger2
@EnableSwagger2
public class SwaggerTest {
    @Bean
    public Docket docket(Environment environment){
        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev");
        //通过environment.acceptsProfiles判断是否处在自己设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);
        System.out.println(flag);
        /**
         * DocumentationType.SWAGGER_2：点进Docket可以发现构造方法中需要一个参数，
         * 在点进这个参数类DocumentationType就能发现具体的参数了
         */
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                //是否启动swagger，如果参数为false，那么浏览器则禁止访问swagger
                //.enable(true)
                .groupName("郑辉盛")
                //.select ** .build为一套模板
                .select()
                //RequestHandlerSelectors:配置要扫描接口的方式，
                // basePackage：可以指定要扫描的包
                // any:扫描全部
                //none：不扫描
                //withMethodAnnotation:扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.example.demo"))
                //过滤什么路径
                //.paths(PathSelectors.ant("/demo/**"))
                .build();
    }

    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("a");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("b");
    }
    @Bean
    public Docket docket4(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("c");
    }
    @Bean
    public Docket docket5(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("d");
    }
    //配置swagger信息apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("郑辉盛","","1652724084@qq.com");
        return new ApiInfo(
                "郑辉盛的swagger文档",    //标题
                "即使再小的帆也能远航",    //描述
                "1.0",                      //版本
                "https://blog.csdn.net/zhenghuishengq",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
